Ban keywords from crate names
authorStephen Becker IV <github@deathbyescalator.com>
Tue, 17 May 2016 05:37:05 +0000 (22:37 -0700)
committerStephen Becker IV <github@deathbyescalator.com>
Tue, 17 May 2016 16:15:26 +0000 (09:15 -0700)
Dearest Reviewer,

This pull request extends the banned list of project names to include
all of the current keywords for rust. I got the list of keywords from
https://doc.rust-lang.org/grammar.html#keywords . This
closes #2699 . The original ticket said warn but I figured how test is
handled is most likely how all banned names should be handled.

Oddly enough a few keywords have made their way to the crates.io. fn and
proc are both examples I found spot checking keywords. I do not there is
any action to take on those crates.

Thanks
Becker

src/cargo/ops/cargo_new.rs
tests/test_cargo_new.rs

index 2a878452c43a1646fea7ea3ca454e121eca9e0c0..b364f8dea4e592f7bab85059e976423b5cc98f91 100644 (file)
@@ -88,7 +88,21 @@ fn get_name<'a>(path: &'a Path, opts: &'a NewOptions, config: &Config) -> CargoR
 }
 
 fn check_name(name: &str) -> CargoResult<()> {
-    if name == "test" {
+
+    // Ban keywords + test list found at
+    // https://doc.rust-lang.org/grammar.html#keywords
+    let blacklist = ["abstract", "alignof", "as", "become", "box",
+        "break", "const", "continue", "crate", "do",
+        "else", "enum", "extern", "false", "final",
+        "fn", "for", "if", "impl", "in",
+        "let", "loop", "macro", "match", "mod",
+        "move", "mut", "offsetof", "override", "priv",
+        "proc", "pub", "pure", "ref", "return",
+        "self", "sizeof", "static", "struct",
+        "super", "test", "trait", "true", "type", "typeof",
+        "unsafe", "unsized", "use", "virtual", "where",
+        "while", "yield"];
+    if blacklist.contains(&name) {
         bail!("The name `{}` cannot be used as a crate name\n\
                use --name to override crate name",
                name)
index 8a4330d656b91b20183786c42ba1ad06b298f664..88943b75c649cdfcfa006ae28e420f41afce2dc4 100644 (file)
@@ -101,6 +101,14 @@ test!(reserved_name {
 use --name to override crate name"));
 });
 
+test!(keyword_name {
+    assert_that(cargo_process("new").arg("pub"),
+                execs().with_status(101)
+                       .with_stderr("\
+[ERROR] The name `pub` cannot be used as a crate name\n\
+use --name to override crate name"));
+});
+
 test!(rust_prefix_stripped {
     assert_that(cargo_process("new").arg("rust-foo").env("USER", "foo"),
                 execs().with_status(0)